package com.tcfvic.client.tools;

import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;

public class Tools {

	public static final DateTimeFormat formater = DateTimeFormat
			.getFormat("dd/MM/yyyy");
	public static final DateTimeFormat dayFormat = DateTimeFormat
			.getFormat("d");
	public static final DateTimeFormat monthFormat = DateTimeFormat
			.getFormat("MMM");
	public static final DateTimeFormat yearFormat = DateTimeFormat
			.getFormat("yyyy");

	public static Date parseDate(String date) {

		return formater.parse(date);
	}

	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		return formater.format(date);
	}

	public static String getTodaysYear() {
		return yearFormat.format(new Date());
	}

	public static String getDay(Date date) {
		return dayFormat.format(date);
	}

	public static String getMonth(Date date) {
		return monthFormat.format(date);
	}

	public static String getYear(Date date) {
		return yearFormat.format(date);
	}

	public static ListBox getMonthListBox() {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (Month month : Month.values()) {
			dropBox.addItem(month.getMonth());
		}

		return dropBox;
	}

	public static ListBox getGenderListBox() {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		dropBox.addItem("F");
		dropBox.addItem("M");

		return dropBox;
	}

	public static int getSelectedIndex(ListBox lb, String value) {
		int result = 0;

		if (value != null && !value.isEmpty()) {
			for (int i = 0; i < lb.getItemCount(); i++) {
				if (value.equalsIgnoreCase(lb.getItemText(i))) {
					result = i;
					break;
				}
			}
		}
		return result;
	}

	public static int getSelectedIndexBasedOnItem(ListBox lb, String item) {
		int result = 0;

		if (item != null && !item.isEmpty()) {
			for (int i = 0; i < lb.getItemCount(); i++) {
				if (item.equalsIgnoreCase(lb.getValue(i))) {
					result = i;
					break;
				}
			}
		}
		return result;
	}

	public static ListBox getDayListBox() {
		return getNumbersListBox(1, 31);
	}
	
	public static ListBox getNumbersListBox(int startNum, int count) {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (int i = startNum; i <= count; i++) {
			dropBox.addItem(String.valueOf(i), String.valueOf(i));
		}
		return dropBox;
	}

	public static ListBox getYearListBox() {
		return getYearListBox(1900);
	}

	public static ListBox getYearListBox(int startYear) {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (int i = startYear; i <= Integer.parseInt(Tools.getTodaysYear()); i++) {
			dropBox.addItem(String.valueOf(i), String.valueOf(i));
		}

		return dropBox;
	}

	public static boolean isValidDate(ListBox day, ListBox month, ListBox year) {
		try {
			parseDate(day, month, year);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static Date parseDate(ListBox day, ListBox month, ListBox year)
			throws Exception {
		if ((day.getItemText(day.getSelectedIndex()) != null && day
				.getItemText(day.getSelectedIndex()).trim().length() > 0)
				|| (month.getItemText(month.getSelectedIndex()) != null && month
						.getItemText(month.getSelectedIndex()).trim().length() > 0)
				|| (year.getItemText(year.getSelectedIndex()) != null && year
						.getItemText(year.getSelectedIndex()).trim().length() > 0)) {
			return Tools
					.parseDate(day.getItemText(day.getSelectedIndex())
							+ "/"
							+ Month
									.valueOf(
											month.getItemText(month
													.getSelectedIndex()))
									.getId() + "/"
							+ year.getItemText(year.getSelectedIndex()));
		}

		throw new Exception(
				"Date supplied is not valid, list box index values "
						+ "of dd mm yy are " + day.getSelectedIndex() + " "
						+ month.getSelectedIndex() + " "
						+ year.getSelectedIndex());

	}

	public static boolean checkIfAnyAreNotNull(TextBox... textboxs) {
		for (TextBox textbox : textboxs) {
			if (textbox.getText() != null
					&& textbox.getText().trim().length() > 0) {
				return true;
			}
		}
		return false;
	}

	public static boolean checkIfSelectedValue(ListBox... listboxs) {
		for (ListBox listbox : listboxs) {
			if (listbox.getSelectedIndex() > -1
					&& listbox.getItemText(listbox.getSelectedIndex()) != null
					&& listbox.getItemText(listbox.getSelectedIndex()).trim()
							.length() > 0) {
				return true;
			}
		}
		return false;
	}

	public static boolean checkIfContainsValues(ListBox... listboxs) {
		for (ListBox listbox : listboxs) {
			if (listbox.getItemCount() > 0) {
				return true;
			}
		}
		return false;
	}

	public static void resetList(ListBox... listboxs) {
		for (ListBox listbox : listboxs) {
			if (listbox != null) { 
				listbox.setSelectedIndex(-1);
			}
		}
	}

	public static void clearList(ListBox... listboxs) {
		for (ListBox listbox : listboxs) {
			listbox.clear();
		}
	}

	public static ListBox getYesNoList() {
		ListBox lb = new ListBox();
		lb.addItem("","");
		lb.addItem("Yes","Yes");
		lb.addItem("No","No");
		return lb;
	}
	
	public static ListBox getYesList() {
		ListBox lb = new ListBox();
		lb.addItem("","");
		lb.addItem("Yes","Yes");
		return lb;
	}
	
	public static ListBox getTrueFalseList() {
		ListBox lb = new ListBox();
		lb.addItem("","");
		lb.addItem("True","True");
		lb.addItem("False","False");
		return lb;
	}
	
	public static ListBox getNatureOfInjuryList() {
		ListBox lb = new ListBox();
		lb.addItem("","");
		lb.addItem("injury - back");
		lb.addItem("injury - leg");
		lb.addItem("injury - foot");
		lb.addItem("injury - arm");
		lb.addItem("injury - hand");
		lb.addItem("impairment - vision");
		lb.addItem("impairment - hearing");
		lb.addItem("illness - permanent");
		lb.addItem("illness - temporary");
		return lb;
	}
	
	public static ListBox getCountryList() {
		String[] countries = new String[] { "Afghanistan", "Aland Islands",
				"Albania", "Algeria", "American Samoa", "Andorra", "Angola",
				"Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina",
				"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
				"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
				"Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
				"Bolivia, Plurinational State Of", "Bosnia And Herzegovina",
				"Botswana", "Bouvet Island", "Brazil",
				"British Indian Ocean Territory", "Brunei Darussalam",
				"Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon",
				"Canada", "Cape Verde", "Cayman Islands",
				"Central African Republic", "Chad", "Chile", "China",
				"Christmas Island", "Cocos (Keeling) Islands", "Colombia",
				"Comoros", "Congo", "Congo, The Democratic Republic Of The",
				"Cook Islands", "Costa Rica", "Cote D'Ivoire", "Croatia",
				"Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti",
				"Dominica", "Dominican Republic", "Ecuador", "Egypt",
				"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
				"Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands",
				"Fiji", "Finland", "France", "French Guiana",
				"French Polynesia", "French Southern Territories", "Gabon",
				"Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
				"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
				"Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
				"Heard Island And Mcdonald Islands",
				"Holy See (Vatican City State)", "Honduras", "Hong Kong",
				"Hungary", "Iceland", "India", "Indonesia",
				"Iran, Islamic Republic Of", "Iraq", "Ireland", "Isle Of Man",
				"Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan",
				"Kazakhstan", "Kenya", "Kiribati",
				"Korea, Democratic People'S Republic Of", "Korea, Republic Of",
				"Kuwait", "Kyrgyzstan", "Lao People'S Democratic Republic",
				"Latvia", "Lebanon", "Lesotho", "Liberia",
				"Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania",
				"Luxembourg", "Macao",
				"Macedonia, The Former Yugoslav Republic Of", "Madagascar",
				"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
				"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
				"Mayotte", "Mexico", "Micronesia, Federated States Of",
				"Moldova, Republic Of", "Monaco", "Mongolia", "Montenegro",
				"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
				"Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
				"New Caledonia", "New Zealand", "Nicaragua", "Niger",
				"Nigeria", "Niue", "Norfolk Island",
				"Northern Mariana Islands", "Norway", "Oman", "Pakistan",
				"Palau", "Palestinian Territory, Occupied", "Panama",
				"Papua New Guinea", "Paraguay", "Peru", "Philippines",
				"Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar",
				"Reunion", "Romania", "Russian Federation", "Rwanda",
				"Saint Barthelemy", "Saint Helena", "Saint Kitts And Nevis",
				"Saint Lucia", "Saint Martin", "Saint Pierre And Miquelon",
				"Saint Vincent And The Grenadines", "Samoa", "San Marino",
				"Sao Tome And Principe", "Saudi Arabia", "Senegal", "Serbia",
				"Seychelles", "Sierra Leone", "Singapore", "Slovakia",
				"Slovenia", "Solomon Islands", "Somalia", "South Africa",
				"South Georgia And The South Sandwich Islands", "Spain",
				"Sri Lanka", "Sudan", "Suriname", "Svalbard And Jan Mayen",
				"Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
				"Taiwan, Province Of China", "Tajikistan",
				"Tanzania, United Republic Of", "Thailand", "Timor-Leste",
				"Togo", "Tokelau", "Tonga", "Trinidad And Tobago", "Tunisia",
				"Turkey", "Turkmenistan", "Turks And Caicos Islands", "Tuvalu",
				"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
				"United States", "United States Minor Outlying Islands",
				"Uruguay", "Uzbekistan", "Vanuatu",
				"Venezuela, Bolivarian Republic Of", "Viet Nam",
				"Virgin Islands, British", "Virgin Islands, U.S.",
				"Wallis And Futuna", "Western Sahara", "Yemen", "Zambia",
				"Zimbabwe" };
		ListBox lb = new ListBox();
		lb.addItem("", "");
		lb.addItem("China", "China");
		lb.addItem("Greece", "Greece");
		lb.addItem("Macedonia", "Macedonia");
		lb.addItem("Philipines", "Philipines");
		lb.addItem("Portugal", "Portugal");
		lb.addItem("Thailand", "Thailand");
		lb.addItem("Vietnam", "Vietnam");
		for (String s : countries) {
			lb.addItem(s, s);
		}
		return lb;
	}
	
	public static ListBox getLanguageList() {
		String[] languages = new String[] {"Afghanistan", "Aland Islands",
				"Albania", "Algeria", "American Samoa", "Andorra", "Angola",
				"Anguilla", "Antarctica", "Antigua And Barbuda", "Argentina",
				"Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
				"Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus",
				"Belgium", "Belize", "Benin", "Bermuda", "Bhutan",
				"Bolivia, Plurinational State Of", "Bosnia And Herzegovina",
				"Botswana", "Bouvet Island", "Brazil",
				"British Indian Ocean Territory", "Brunei Darussalam",
				"Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon",
				"Canada", "Cape Verde", "Cayman Islands",
				"Central African Republic", "Chad", "Chile", "China",
				"Christmas Island", "Cocos (Keeling) Islands", "Colombia",
				"Comoros", "Congo", "Congo, The Democratic Republic Of The",
				"Cook Islands", "Costa Rica", "Cote D'Ivoire", "Croatia",
				"Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti",
				"Dominica", "Dominican Republic", "Ecuador", "Egypt",
				"El Salvador", "Equatorial Guinea", "Eritrea", "Estonia",
				"Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands",
				"Fiji", "Finland", "France", "French Guiana",
				"French Polynesia", "French Southern Territories", "Gabon",
				"Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece",
				"Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala",
				"Guernsey", "Guinea", "Guinea-Bissau", "Guyana", "Haiti",
				"Heard Island And Mcdonald Islands",
				"Holy See (Vatican City State)", "Honduras", "Hong Kong",
				"Hungary", "Iceland", "India", "Indonesia",
				"Iran, Islamic Republic Of", "Iraq", "Ireland", "Isle Of Man",
				"Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan",
				"Kazakhstan", "Kenya", "Kiribati",
				"Korea, Democratic People'S Republic Of", "Korea, Republic Of",
				"Kuwait", "Kyrgyzstan", "Lao People'S Democratic Republic",
				"Latvia", "Lebanon", "Lesotho", "Liberia",
				"Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania",
				"Luxembourg", "Macao",
				"Macedonia, The Former Yugoslav Republic Of", "Madagascar",
				"Malawi", "Malaysia", "Maldives", "Mali", "Malta",
				"Marshall Islands", "Martinique", "Mauritania", "Mauritius",
				"Mayotte", "Mexico", "Micronesia, Federated States Of",
				"Moldova, Republic Of", "Monaco", "Mongolia", "Montenegro",
				"Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia",
				"Nauru", "Nepal", "Netherlands", "Netherlands Antilles",
				"New Caledonia", "New Zealand", "Nicaragua", "Niger",
				"Nigeria", "Niue", "Norfolk Island",
				"Northern Mariana Islands", "Norway", "Oman", "Pakistan",
				"Palau", "Palestinian Territory, Occupied", "Panama",
				"Papua New Guinea", "Paraguay", "Peru", "Philippines",
				"Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar",
				"Reunion", "Romania", "Russian Federation", "Rwanda",
				"Saint Barthelemy", "Saint Helena", "Saint Kitts And Nevis",
				"Saint Lucia", "Saint Martin", "Saint Pierre And Miquelon",
				"Saint Vincent And The Grenadines", "Samoa", "San Marino",
				"Sao Tome And Principe", "Saudi Arabia", "Senegal", "Serbia",
				"Seychelles", "Sierra Leone", "Singapore", "Slovakia",
				"Slovenia", "Solomon Islands", "Somalia", "South Africa",
				"South Georgia And The South Sandwich Islands", "Spain",
				"Sri Lanka", "Sudan", "Suriname", "Svalbard And Jan Mayen",
				"Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic",
				"Taiwan, Province Of China", "Tajikistan",
				"Tanzania, United Republic Of", "Thailand", "Timor-Leste",
				"Togo", "Tokelau", "Tonga", "Trinidad And Tobago", "Tunisia",
				"Turkey", "Turkmenistan", "Turks And Caicos Islands", "Tuvalu",
				"Uganda", "Ukraine", "United Arab Emirates", "United Kingdom",
				"United States", "United States Minor Outlying Islands",
				"Uruguay", "Uzbekistan", "Vanuatu",
				"Venezuela, Bolivarian Republic Of", "Viet Nam",
				"Virgin Islands, British", "Virgin Islands, U.S.",
				"Wallis And Futuna", "Western Sahara", "Yemen", "Zambia",
				"Zimbabwe" };
		ListBox lb = new ListBox();
		lb.addItem("","");
		lb.addItem("English","English");
		lb.addItem("Cantonese","Cantonese");
		lb.addItem("Croatian","Croatian");
		lb.addItem("Greek","Greek");
		lb.addItem("Hakka","Hakka");
		lb.addItem("Hindi","Hindi");
		lb.addItem("Italian","Italian");
		lb.addItem("Khmer","Khmer");
		lb.addItem("Macedonian","Macedonian");
		lb.addItem("Mandarin","Mandarin");
		lb.addItem("Polish","Polish");
		lb.addItem("Portuguese","Portuguese");
		lb.addItem("Slovenian","Slovenian");
		lb.addItem("Spanish","Spanish");
		lb.addItem("Tagalog","Tagalog");
		lb.addItem("Tamil","Tamil");
		lb.addItem("Thai","Thai");
		lb.addItem("Turkish","Turkish");
		lb.addItem("Urdu","Urdu");
		lb.addItem("Vietnamese","Vietnamese");
		lb.addItem("-------------------","-------------------");
		for (String s : languages) {
			lb.addItem(s, s);
		}
		return lb;
	}
	
	
	public static ListBox populateListBoxWithNumberRange(int start, int stop) {
		ListBox lb = new ListBox();
		lb.addItem("", "");
		for (int i = start; i <= stop; i++) {
			lb.addItem(String.valueOf(i), String.valueOf(i));
		}
		return lb;
	}

	public static ListBox getStatesListBox() {
		final ListBox dropBox = new ListBox(false);

		dropBox.addItem("");
		for (State state : State.values()) {
			dropBox.addItem(state.getState());
		}
		return dropBox;
	}

	public static CourseType getCourseType(String courseType) {
		for (CourseType type : CourseType.values()) {
			if (type.getType().equalsIgnoreCase(courseType) ||
					type.toString().equals(courseType)) {
				return type;
			}
		}
		return null;
	}

	public static ListBox getIndustryList(String industry) {
		ListBox lb = new ListBox();
		lb.addItem("", "");
		lb.addItem("Administration", "Administration");
		lb.addItem("Aged care", "Aged care");
		lb.addItem("Beauty", "Beauty");
		lb.addItem("Business", "Business");
		lb.addItem("Child care", "Child care");
		lb.addItem("ESL", "ESL");
		lb.addItem("Food processing", "Food processing");
		lb.addItem("Health", "Health");
		lb.addItem("Horticulture", "Horticulture");
		lb.addItem("Hospitality", "Hospitality");
		lb.addItem("Information technology", "Information technology");
		lb.addItem("Logistics", "Logistics");
		lb.addItem("Manufacturing other", "Manufacturing other");
		lb.addItem("Manufacturing TCF", "Manufacturing TCF");
		lb.addItem("Retail", "Retail");
		lb.addItem("Transport", "Transport");
		if (industry != null && !industry.isEmpty()) {
			lb.setSelectedIndex(Tools.getSelectedIndex(lb, industry));
		}
		return lb;
	}

}
